package com.wuye.controller;

import com.wuye.model.User;
import com.wuye.repository.UserRepository;
import com.wuye.service.UserService;
import com.wuye.web.ST;
import com.wuye.web.SessionHandler;
import com.wuye.web.UserSession;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.*;

import org.apache.commons.logging.*;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SessionHandler sessionHandler;
    private Log log = LogFactory.getLog(this.getClass());

    @RequestMapping(value = "/demo.jspx")
    public ModelAndView checkLogin(@RequestParam(value = "haha", required = false) String haha) {
        log.info("---" + haha);
        ModelAndView view = new ModelAndView("/index.html");
        return view;
    }

    @RequestMapping(value = "/loginAction.jspx")
    public String checkUserByPhone(String mobile, String password, HttpSession session) {
        try {
            User user = userService.checkUser(mobile);
            String status = "";
            if (user == null) {
                status = ST.userNotExist.toJson();
                return status;
            } else if (!user.getPassword().equals(password)) {
                status = ST.loginWrongPassword.toJson();
                return status;
            } else {
                UserSession us = sessionHandler.getUserSession(session);
                us.setUserId(user.getUserId());
                us.setCellPhone(user.getCellPhone());
                us.setUserName(user.getUserName());
                sessionHandler.saveUserSession(us, session);
                JSONObject json = ST.ok.getJsonObject();
                json.put("user", us);
                return json.toString();
            }
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/resentPassword.jspx")
    public String checkPassword(String password, String mobile, HttpSession session) {
        log.info("---loginTo");
        try {
            userService.modifyPassword(password, mobile);
            String status = ST.ok.toJson();
            sessionHandler.removeSession(session);
            return status;
        } catch (RuntimeException e) {
            return e.getMessage();
        }
    }

    @RequestMapping("/loginOut.jspx")
    public String loginOut(HttpSession session) {
        log.info("---loginTo");
        sessionHandler.removeSession(session);
        String status = ST.ok.toJson();
        return status;
    }

    @RequestMapping("/saveUser.jspx")
    public String saveUser(User user) {
        String cellPhone = user.getCellPhone();
        User users = userService.checkUser(cellPhone);
        if (users == null) {
            userService.saveUser(user);
            return ST.ok.toJson();
        } else {
            return ST.userExist.toJson();
        }

    }
    @RequestMapping("/userInfo.jspa")
    public User userInfo(@RequestParam("userId") String userId) {
        return userRepository.findByUserId(userId);
    }
    @RequestMapping("/saveUserInfo.jspa")
    public String saveUserInfo(@RequestParam("userId") String userId,@RequestParam("phone") String phone,@RequestParam("roomNum") String roomNum) {
        User user = userRepository.findByUserId(userId);
        user.setDeskPhone(phone);
        user.setRoom(roomNum);
        userRepository.save(user);
        return ST.ok.toJson();
    }


}